//
//  MBSpaceIPADMyParentChildShowRemoteCell.m
//  MiBaoTu
//
//  Created by admin on 16/10/10.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceIPADMyParentChildShowLocalCell.h"

@interface MBSpaceIPADMyParentChildShowLocalCell (){
    UIView                      *_bgView;
    UIImageView                 *_contentImgView;
    UILabel                     *_titleLb;
    UIImageView                 *_zhuboImgView;
    UILabel                     *_zhuboLb;

    UIButton                    *_sendBt;
    UIButton                    *_deleteBt;
    
    CAShapeLayer                *lineShaper;
    CATextLayer                 *dTextLayer;
    CATextLayer                 *mTextLayer;
    CAShapeLayer                *squreShaper;
    CAShapeLayer                *btShaper;
    CATextLayer                 *sendTextLayer;
    CAShapeLayer                *deletShaper;
    CATextLayer                 *deleteTextLayer;
}

@end
@implementation MBSpaceIPADMyParentChildShowLocalCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    //图片
    _contentImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _contentImgView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_contentImgView];
    
    //标题
    _titleLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _titleLb.backgroundColor = [UIColor clearColor];
    _titleLb.textAlignment = NSTextAlignmentLeft;
    _titleLb.textColor = UIColorFromHex(0x333333);
    _titleLb.font = [UIFont systemFontOfSize:13.f];
    [self.contentView addSubview:_titleLb];
    
    //主播
    _zhuboImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _zhuboImgView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_zhuboImgView];
    
    _zhuboLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _zhuboLb.textAlignment = NSTextAlignmentRight;
    _zhuboLb.textColor = UIColorFromHex(0x999999);
    _zhuboLb.font = [UIFont systemFontOfSize:12.f];
    [self.contentView addSubview:_zhuboLb];
    
    //按钮
    _sendBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _sendBt.backgroundColor = [UIColor whiteColor];
    [_sendBt setTitle:@"发布" forState:UIControlStateNormal];
    [_sendBt setTitle:@"发布" forState:UIControlStateHighlighted];
    [_sendBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_sendBt setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    _sendBt.titleLabel.font = _titleLb.font;
    [_sendBt addTarget:self action:@selector(onSelectSendInformation) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_sendBt];
    
    _deleteBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteBt.backgroundColor = [UIColor whiteColor];
    [_deleteBt setTitle:@"删除" forState:UIControlStateNormal];
    [_deleteBt setTitle:@"删除" forState:UIControlStateHighlighted];
    [_deleteBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_deleteBt setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    _deleteBt.titleLabel.font = _sendBt.titleLabel.font;
    [_deleteBt addTarget:self action:@selector(onSelectDeleteInformation) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_deleteBt];

}

//发布
- (void)onSelectSendInformation
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForIPADSendInformationWithValue:withView:)]) {
        [_delegate onSelectItemForIPADSendInformationWithValue:@"" withView:self];
    }
}

//删除
- (void)onSelectDeleteInformation
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForIPADDelegateInformationWithValue:withView:)]) {
        [_delegate onSelectItemForIPADDelegateInformationWithValue:@"" withView:self];
    }
}


- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
    [self setNeedsDisplay];
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    [self setNeedsDisplay];
    [self setNeedsLayout];
}

- (void)setTimeShow:(NSString *)timeShow
{
    _timeShow = timeShow;
    [self setNeedsDisplay];
    [self setNeedsLayout];
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    float radio = (_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
    float qleft = 72*PERCEN*radio;
    float qtop = 50*PERCEN*radio;
    float qWidth = 120*PERCEN*radio;
    float qHeight = 120*PERCEN*radio;
    UIBezierPath *lineBezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(qleft, qtop, qWidth, qHeight) cornerRadius:qHeight/2.];
    if (!lineShaper) {
        lineShaper = [CAShapeLayer layer];
    } else {
        [lineShaper removeFromSuperlayer];
    }
    lineShaper.path = lineBezierPath.CGPath;
    lineShaper.lineWidth = 1.;
    lineShaper.lineCap = kCALineCapSquare;
    lineShaper.lineJoin = kCALineJoinRound;
    lineShaper.strokeColor = [UIColorFromHex(0xeb64a7) colorWithAlphaComponent:1.].CGColor;
    lineShaper.fillColor = [UIColor clearColor].CGColor;
    [self.layer addSublayer:lineShaper];
    
    if (_timeShow.length > 0) {
        NSArray *timesArr = [_timeShow componentsSeparatedByString:@"-"];
        
        //日期
        NSString *dString = [timesArr lastObject];
        UIFont *dfont = [UIFont systemFontOfSize:20.f*radio];
        if (!dTextLayer) {
            dTextLayer = [CATextLayer layer];
        } else {
            [dTextLayer removeFromSuperlayer];
        }
        dTextLayer.string = dString;
        dTextLayer.frame = CGRectMake(qleft,qtop+20*PERCEN*radio,qWidth,20);
        dTextLayer.font = CGFontCreateWithFontName((__bridge CFStringRef)dfont.fontName);
        dTextLayer.fontSize = dfont.pointSize;
        dTextLayer.foregroundColor = UIColorFromHex(0xeb64a7).CGColor;
        dTextLayer.wrapped = YES;
        dTextLayer.alignmentMode = kCAAlignmentCenter;
        dTextLayer.contentsScale = [UIScreen mainScreen].scale;
        [self.layer addSublayer:dTextLayer];
        
        //月份
        NSString *months = timesArr[1];
        NSString *mString = @"十二月";
        if ([months intValue] == 1) {
            mString = @"一月";
        }
        else  if ([months intValue] == 2) {
            mString = @"二月";
        }
        else  if ([months intValue] == 3) {
            mString = @"三月";
        }
        else  if ([months intValue] == 4) {
            mString = @"四月";
        }
        else  if ([months intValue] == 5) {
            mString = @"五月";
        }
        else  if ([months intValue] == 6) {
            mString = @"六月";
        }
        else  if ([months intValue] == 7) {
            mString = @"七月";
        }
        else  if ([months intValue] == 8) {
            mString = @"八月";
        }
        else  if ([months intValue] == 9) {
            mString = @"九月";
        }
        else  if ([months intValue] == 10) {
            mString = @"十月";
        }
        else  if ([months intValue] == 11) {
            mString = @"十一月";
        }
        else  if ([months intValue] == 12) {
            mString = @"十二月";
        }
        UIFont *mfont = [UIFont systemFontOfSize:12.f*radio];
        if (!mTextLayer) {
            mTextLayer = [CATextLayer layer];
        } else {
            [mTextLayer removeFromSuperlayer];
        }
        mTextLayer.string = mString;
        mTextLayer.frame = CGRectMake(qleft,qtop+20*PERCEN*radio+20*radio,qWidth,20);
        mTextLayer.font = CGFontCreateWithFontName((__bridge CFStringRef)mfont.fontName);
        mTextLayer.fontSize = mfont.pointSize;
        mTextLayer.foregroundColor = UIColorFromHex(0xeb64a7).CGColor;
        mTextLayer.wrapped = YES;
        mTextLayer.alignmentMode = kCAAlignmentCenter;
        mTextLayer.contentsScale = [UIScreen mainScreen].scale;
        [self.layer addSublayer:mTextLayer];
        
    } else {
        [dTextLayer removeFromSuperlayer];
        [mTextLayer removeFromSuperlayer];
    }
   
    
    //画多边框框
    float beleft = qleft+qWidth+30*PERCEN*radio;
    float bexLine = 30*PERCEN*radio;
    float bebottom = qtop+548*PERCEN*radio;
    float beWidth = _bgView.width-50*PERCEN*radio-beleft-bexLine;
    UIBezierPath *squreBezierPath = [UIBezierPath bezierPath];
    [squreBezierPath moveToPoint:CGPointMake(beleft, qtop)];
    [squreBezierPath addLineToPoint:CGPointMake(beleft+bexLine, qtop+bexLine)];
    [squreBezierPath addLineToPoint:CGPointMake(beleft+bexLine, bebottom)];
    [squreBezierPath addLineToPoint:CGPointMake(beleft+bexLine+beWidth, bebottom)];
    [squreBezierPath addLineToPoint:CGPointMake(beleft+bexLine+beWidth, qtop)];
    [squreBezierPath addLineToPoint:CGPointMake(beleft, qtop)];
    [squreBezierPath closePath];
    if (!squreShaper) {
        squreShaper = [CAShapeLayer layer];
    } else {
        [squreShaper removeFromSuperlayer];
    }
    squreShaper.path = squreBezierPath.CGPath;
    squreShaper.strokeColor = [UIColor clearColor].CGColor;//[UIColorFromHex(0x999999) colorWithAlphaComponent:.15].CGColor;
    squreShaper.fillColor = [UIColorFromHex(0x999999) colorWithAlphaComponent:.15].CGColor;
    [self.layer addSublayer:squreShaper];
    
    //按钮
    float btWidth = (beWidth - 30*2*PERCEN-40*PERCEN)/2.;
    float btHeight = 90*PERCEN;
    UIBezierPath *btBezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(beleft+bexLine+30*PERCEN, bebottom-20*PERCEN-btHeight, btWidth, btHeight)];
    if (!btShaper) {
        btShaper = [CAShapeLayer layer];
    } else {
        [btShaper removeFromSuperlayer];
    }
    btShaper.path = btBezierPath.CGPath;
    btShaper.fillColor = [UIColor whiteColor].CGColor;
    btShaper.strokeColor = [UIColorFromHex(0x999999) colorWithAlphaComponent:.15].CGColor;
    [self.layer addSublayer:btShaper];
    
    UIFont *sendfont = _titleLb.font;
    if (!sendTextLayer) {
        sendTextLayer = [CATextLayer layer];
    } else {
        [sendTextLayer removeFromSuperlayer];
    }
    sendTextLayer.string = _sendBt.titleLabel.text;
    sendTextLayer.frame = CGRectMake(beleft+bexLine+30*PERCEN, bebottom-20*PERCEN-3*btHeight/4., btWidth, btHeight);
    sendTextLayer.font = CGFontCreateWithFontName((__bridge CFStringRef)sendfont.fontName);
    sendTextLayer.fontSize = sendfont.pointSize;
    sendTextLayer.foregroundColor = UIColorFromHex(0x333333).CGColor;
    sendTextLayer.wrapped = YES;
    sendTextLayer.alignmentMode = kCAAlignmentCenter;
    sendTextLayer.truncationMode = kCATruncationMiddle;
    sendTextLayer.contentsScale = [UIScreen mainScreen].scale;
    [self.layer addSublayer:sendTextLayer];
    
    UIBezierPath *liBezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(beleft+bexLine+70*PERCEN+btWidth, bebottom-20*PERCEN-btHeight, btWidth, btHeight)];;
    if (!deletShaper) {
        deletShaper = [CAShapeLayer layer];
    } else {
        [deletShaper removeFromSuperlayer];
    }
    deletShaper.path = liBezierPath.CGPath;
    deletShaper.fillColor = [UIColor whiteColor].CGColor;
    deletShaper.strokeColor = [UIColorFromHex(0x999999) colorWithAlphaComponent:.15].CGColor;
    [self.layer addSublayer:deletShaper];
    
    if (!deleteTextLayer) {
        deleteTextLayer = [CATextLayer layer];
    } else {
        [deleteTextLayer removeFromSuperlayer];
    }
    deleteTextLayer.string = _deleteBt.titleLabel.text;
    deleteTextLayer.frame = CGRectMake(beleft+bexLine+70*PERCEN+btWidth,bebottom-20*PERCEN-3*btHeight/4., btWidth, btWidth);
    deleteTextLayer.font = CGFontCreateWithFontName((__bridge CFStringRef)sendfont.fontName);
    deleteTextLayer.fontSize = sendfont.pointSize;
    deleteTextLayer.foregroundColor = UIColorFromHex(0x333333).CGColor;
    deleteTextLayer.wrapped = YES;
    deleteTextLayer.alignmentMode = kCAAlignmentCenter;
    deleteTextLayer.contentsScale = [UIScreen mainScreen].scale;
    [self.layer addSublayer:deleteTextLayer];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //value
    if (_dataDic) {
        NSString *picPath = [_dataDic objectForKey:@"picture"];
        if (picPath.length > 0) {
            NSString *picName = [[picPath componentsSeparatedByString:@"/"] lastObject];
            picPath = [MusicCachesDirectory stringByAppendingPathComponent:picName];
            _contentImgView.image = [self getCenterImg:[UIImage imageWithContentsOfFile:picPath]];
        } else {
            _contentImgView.image = [UIImage imageNamed:@"qzx_player_bg"];//[self getCenterImg:[UIImage imageNamed:@"qzx_player_bg"]];
        }
        _contentImgView.image = [self getCenterImg:[UIImage imageWithContentsOfFile:picPath]];
        _titleLb.text = [_dataDic objectForKey:@"title"];
        NSString *type = [_dataDic objectForKey:@"type"];
        if ([type isEqualToString:@"1"]) {
            _zhuboLb.text = @"宝贝秀";
        } else {
            _zhuboLb.text = @"主播秀";
        }
    }
    UIImage *img = [UIImage imageNamed:@"biaoqian"];
    _zhuboImgView.image = img;

    
    //frame
    float radio = (_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
    float qleft = 72*PERCEN*radio;
    float qtop = 50*PERCEN*radio;
    float qWidth = 120*PERCEN*radio;
    
    float beleft = qleft+qWidth+30*PERCEN*radio;
    float bexLine = 30*PERCEN*radio;
    float bebottom = qtop+548*PERCEN*radio;
    float beWidth = _bgView.width-50*PERCEN*radio-beleft-bexLine;
    _bgView.frame = CGRectMake(0, 0, self.width, self.height);
    
    _contentImgView.frame = CGRectMake(beleft+bexLine+30*PERCEN*radio, qtop+30*PERCEN*radio, beWidth-60*PERCEN*radio, bebottom-qtop-218*PERCEN);
    
    _titleLb.frame = CGRectMake(_contentImgView.left, _contentImgView.bottom+10*PERCEN, 140, 20);
    
    float zhuboWidth = [_zhuboLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_zhuboLb.font} context:NULL].size.width;
    _zhuboLb.frame = CGRectMake(_contentImgView.right-zhuboWidth, _titleLb.top, zhuboWidth, 20);
    _zhuboImgView.frame = CGRectMake(_zhuboLb.left-10*PERCEN-img.size.width, _zhuboLb.top+5, img.size.width, img.size.height);
    
    float btHeight = bebottom-qtop-_titleLb.bottom-50*PERCEN;
    float btWidth = (beWidth - 30*2*PERCEN-40*PERCEN)/2.;
    _sendBt.frame = CGRectMake(beleft+bexLine+30*PERCEN, bebottom-20*PERCEN-btHeight, btWidth, btHeight);
    _deleteBt.frame =CGRectMake(beleft+bexLine+70*PERCEN+btWidth, bebottom-20*PERCEN-btHeight, btWidth, btHeight);
    _deleteBt.backgroundColor=[UIColor redColor];
    _sendBt.backgroundColor=[UIColor cyanColor];
     NSLog(@"%@",NSStringFromCGRect(_deleteBt.frame));
}

- (UIImage *)getCenterImg:(UIImage *)image
{
    CGImageRef imageRef = image.CGImage;
    float imgHeight = 1.0 * image.size.width * _contentImgView.height/_contentImgView.width;
    float rectHeight = imgHeight;
    float top = (image.size.height-imgHeight)/2.;
    CGRect rect;
    if (top > 0) {
        //图片实际高度比UI高度高
        if (imgHeight > image.size.height) {
            rectHeight = image.size.height;
            top = (imgHeight - image.size.height)/2.;
        }
        rect = CGRectMake(0, top, image.size.width, rectHeight);
    }
    else {
        //UI高度比图片实际高度高
        float imgWidth = 1.0 * image.size.height * _contentImgView.width/_contentImgView.height;
        float rectWidth = imgWidth;
        float left = (image.size.width-imgWidth)/2.;
        if (imgWidth > image.size.width) {
            rectWidth = image.size.width;
            left = (imgWidth - image.size.width)/2.;
        }
        rect = CGRectMake(left, 0, rectWidth, image.size.height);
    }
    CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *rectImg = [UIImage imageWithCGImage:imagePartRef];
    CGImageRelease(imagePartRef);
    return rectImg;
}

@end
